package Demo;

import java.io.Serializable;

public class Goods implements Serializable {
    private static final long serialVersionUID = 1418928739237864409L;
    private String brand;
    private String name;
    private boolean flag=false;

    public Goods(String brand, String name) {
        this.brand = brand;
        this.name = name;
    }

    public Goods() {
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public synchronized  void set(String brand,String name){
        if (this.isFlag()){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.setBrand(brand);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.setName(name);
        System.out.println("生产者生产了"+brand+"--"+name);
        this.setFlag(true);
        notify();
    }

    public synchronized void get() {
        if (this.isFlag()==false){
            try {
                 wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("消费者取走了"+this.getBrand()+"-----"+this.getName());

        this.setFlag(false);
        notify();

    }

}
